package com.tebiecloud.store.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.tebiecloud.common.utils.DateUtils;
import com.tebiecloud.common.utils.RedisUtils;
import com.tebiecloud.common.utils.UUIDUtil;
import com.tebiecloud.store.client.constatns.StoreConstants;
import com.tebiecloud.store.client.model.entity.Store;
import com.tebiecloud.store.client.model.entity.StoreFlow;
import com.tebiecloud.store.server.mapper.StoreFlowMapper;
import com.tebiecloud.store.server.mapper.StoreMapper;
import com.tebiecloud.store.server.service.StoreFlowService;
import com.tebiecloud.common.mybatis.base.service.impl.BaseServiceImpl;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * 门店流水 服务实现类
 * @author tebie
 * @date 2019-09-15
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class StoreFlowServiceImpl extends BaseServiceImpl<StoreFlowMapper, StoreFlow> implements StoreFlowService {

    @Autowired
    RedisUtils redisUtils;
    @Autowired
    StoreMapper storeMapper;
    @Autowired
    StoreFlowMapper storeFlowMapper;

    @Override
    public Map<String, String> saveFlow(Long storeId, Long number, int amount, int type) {
        Map<String, String> resultMap = new HashMap<String, String>();
        int time = 10;
        String key = StoreConstants.REDIS_FLOW_+storeId;
        boolean isLock = redisUtils.lock(key, time);
        if(!isLock){
            throw new RuntimeException("资金池被占用，请稍等");
        }
        //查询钱包
        QueryWrapper<Store> storeQueryWrapper = new QueryWrapper<>();
        storeQueryWrapper.eq("store_id",storeId);
        Store store = storeMapper.selectOne(storeQueryWrapper);
        //计算交易的资金
        int futureFunds = 0;
        if(type==1){//订单核销
            futureFunds = store.getBalance() + amount;
        }
        if(type==2){//提现
            futureFunds = store.getBalance() - amount;
        }
        StoreFlow storeFlow = new StoreFlow();
        storeFlow.setTypeId(number);
        storeFlow.setType(type);
        storeFlow.setAmount(amount);
        storeFlow.setYear(Integer.valueOf(DateUtils.formatYear()));
        storeFlow.setMonth(Integer.valueOf(DateUtils.formatMonth()));
        storeFlow.setDay(Integer.valueOf(DateUtils.formatDay()));
        storeFlow.setStoreId(storeId);
        int flowInsert = storeFlowMapper.insert(storeFlow);
        if(flowInsert!=0){
            //更新资金
            UpdateWrapper<Store> storeUpdateWrapper = new UpdateWrapper<>();
            storeUpdateWrapper.eq("store_id",storeId);
            Store store1 = new Store();
            store1.setBalance(futureFunds);
            storeMapper.update(store1,storeUpdateWrapper);
        }
        //解锁
        redisUtils.unLock(key);
        return resultMap;
    }
}
